package org.qloudgen.txn.dilithium.cluster;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.Page;
import org.apache.wicket.Session;
import org.apache.wicket.core.request.mapper.CryptoMapper;
import org.apache.wicket.core.util.crypt.KeyInSessionSunJceCryptFactory;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.IRequestMapper;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
import org.apache.wicket.settings.IApplicationSettings;
import org.qloudgen.txn.dilithium.cluster.common.CommonValues;
import org.qloudgen.txn.dilithium.cluster.pages.ClusterMainPage;
import org.qloudgen.txn.dilithium.cluster.pages.common.AccessDeniedPage;
import org.qloudgen.txn.dilithium.cluster.pages.common.InternalErrorPage;
import org.qloudgen.txn.dilithium.cluster.pages.common.PageExpiredErrorPage;
import org.qloudgen.txn.dilithium.cluster.security.DilithiumAuthorizationStrategy;
import org.qloudgen.txn.dilithium.cluster.security.DilithiumSession;


public class ClusterApplication extends WebApplication{

	static Log log = LogFactory.getLog(ClusterApplication.class);
	 
	@Override
	protected void init() {
		super.init();
		
		// crypt url
		getSecuritySettings().setCryptFactory(new KeyInSessionSunJceCryptFactory());
		IRequestMapper cryptoMapper = new CryptoMapper(getRootRequestMapper(), this);
		setRootRequestMapper(cryptoMapper);
		
		this.getMarkupSettings().setStripWicketTags(true);
		
		// set user-defined error pages
		IApplicationSettings settings = getApplicationSettings();
		settings.setAccessDeniedPage(AccessDeniedPage.class);
		settings.setPageExpiredErrorPage(PageExpiredErrorPage.class);
		settings.setInternalErrorPage(InternalErrorPage.class);	
		
		// access control strategy
		this.getSecuritySettings().setAuthorizationStrategy(new DilithiumAuthorizationStrategy());
		
		/*
		 * initiation of common values 
		 */
		CommonValues.getInstance().init();
		
		log.info("ClusterApplication started!");
	}
	
	@Override
	public Class<? extends Page> getHomePage() {
		return ClusterMainPage.class;
	}
	
	@Override
	public Session newSession(Request request, Response response) {
		return new DilithiumSession(request);   
	} 
	
}
